﻿
using Expressions.Compiler;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests
{
    /// <summary>
    /// Summary description for FloatingPoint
    /// </summary>
    [TestClass]
    public class Test2
    {
        public Test2()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        /// <summary>
        /// Floatings the point test method1. <see cref="http://www.jaggersoft.com/csharp_grammar.html#real-literal"/>
        /// <see cref="http://msdn.microsoft.com/en-us/library/aa664812(VS.71).aspx"/>
        /// </summary>
        [TestMethod]
        public void Test2_1()
        {
            // Forward declarations
            Nonterminal<char> real_literal = "real-literal";
            Nonterminal<char> exponent_part = "exponent-part";
            Nonterminal<char> sign = "sign";
            Nonterminal<char> real_type_suffix = "real-type-suffix";
            Nonterminal<char> decimal_digits = "decimal-digits";
            Nonterminal<char> decimal_digit = "decimal-digit";

            /*
            real-literal::
                sign[opt] decimal-digits . decimal-digits exponent-part[opt] real-type-suffix[opt]
                sign[opt] . decimal-digits exponent-part[opt] real-type-suffix[opt] 
                sign[opt] decimal-digits exponent-part real-type-suffix[opt] 
                sign[opt] decimal-digits real-type-suffix 
            */
            real_literal.Definition = new Rules<char>
            {
                () => sign[true] + decimal_digits[false] + '.' + decimal_digits[false] + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + '.' + decimal_digits[false] + exponent_part[true] + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + exponent_part[false] + real_type_suffix[true],
                () => sign[true] + decimal_digits[false] + real_type_suffix[false]
            };

            /*
            exponent-part::
                e sign[opt] decimal-digits 
                E sign[opt] decimal-digits 
            */
            exponent_part.Definition = new Rules<char>
            {
                () => 'e' + sign[true] + decimal_digits[false],
                () => 'E' + sign[true] + decimal_digits[false]
            };

            /*
            sign:: one of
                + - 
            */
            sign.Definition = new Rules<char>
            {
                () => '+',
                () => '-'
            };

            /*
            real-type-suffix:: one of
                F f  
             */
            real_type_suffix.Definition = new Rules<char>
            {
                () => 'F',
                () => 'f'
            };

            /*
            decimal-digits::
                decimal-digit 
                decimal-digits decimal-digit 
            */
            decimal_digits.Definition = new Rules<char>
            {
                () => decimal_digit[false] + decimal_digits[true]
            };

            /*
            decimal-digit:: one of
                0 1 2 3 4 5 6 7 8 9 
            */
            decimal_digit.Definition = new Rules<char>
            {
                () => '0',
                () => '1',
                () => '2',
                () => '3',
                () => '4',
                () => '5',
                () => '6',
                () => '7',
                () => '8',
                () => '9'
            };

            Nonterminal<char> grammar = "G";
            grammar.Definition = new Rules<char> 
            {
                () => real_literal[false]
            };

            grammar.Initialize();
            grammar.Validate();

            Entity<char> target = grammar;

            Parser<char> input = ".5e-2f".ToCharArray();
            bool expected = true;
            bool actual = target.Parse(input) == true;
            Assert.AreEqual(expected, actual);
        }
    }
}
